<template>
    <div style="height: 95%;width: 100%;display:flex;justify-content:center;padding-top: 20px">
        <!--弹框DIV 设置弹框的大小的地方-->
        <div style="background: #fff;width: 700px;height: 600px;box-shadow: #D3DCE6 0px 0px 20px;">
            <div style="height:520px;border: #1C84C6;margin-top: 25px" class="scrollbar">
                <div style="height: 30px;">
                    <!--标题-->
                    <span style="font-size: 18px;margin-left: 25px;">
                        新建出库单
                    </span>
                    <!--返回列表-->
                        <span @click="tzdz" style="margin-left: 500px;">
                            <i slot="suffix" style="font-size: 30px;color: #D3DCE6" class="el-icon-close"></i>
                        </span>
                </div>
                <!--height:91%;表单在弹框显示多长-->
                <el-scrollbar style="height:91%;padding-top: 20px;">
                    <div class="shugan"></div>
                    <span style="margin-left: 10px;font-size: 14px;">基本信息</span>
                    <br/>
                    <br/>
                    <!--表单-->
                    <!--表单-->
                    <el-form style="margin-top:5px;margin-left: 30px;" size="mini"  :rules="rules" :model="jhlist" ref="jhlist">
                        <el-row>
                            <el-col :span="10">
                                <el-form-item style="width:250px">
                                    <label slot="label" style="font-size: 13px">出库编号：</label><br/>
                                    <el-input v-model="jhlist.chukuNo" :disabled="true"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="8">
                                <el-form-item style="width:250px" prop="chukuTime">
                                    <label slot="label" style="font-size: 13px">出库时间：</label><br/>
                                    <el-date-picker
                                            v-model="jhlist.chukuTime"
                                            type="datetime"
                                            placeholder="选择日期时间"
                                            align="right"
                                            :picker-options="pickerOptions"
                                            style="width:250px">
                                    </el-date-picker>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="10">
                                <el-form-item style="width:250px;" prop="clientId">
                                    <label slot="label" style="font-size: 13px">客户：</label><br/>
                                    <!--客户名称-->
                                    <el-popover
                                            style="width: 700px; height: 450px;"
                                            placement="right"
                                            trigger="click"
                                            v-model="kehu">
                                        <span style="font-size: 16px;">关联客户模块</span>
                                        <br/>
                                        <el-input @keyup.enter.native="cx()" v-model="kehuinput" style="width: 210px; margin-left: 230px; margin-bottom: 10px;">
                                            <i slot="suffix" class="el-input__icon el-icon-search" @click="cx()"></i>
                                        </el-input>
                                        <el-button type="primary" style="margin-left: 130px;">新 建</el-button>
                                        <el-table
                                                highlight-current-row
                                                border
                                                ref="kehulist"
                                                :data="kehulist">
                                            <el-table-column type="selection" width="50" align="center" property="clientId"></el-table-column>
                                            <el-table-column width="150" property="clientName" label="客户名称"></el-table-column>
                                            <el-table-column width="100" property="clientRank" label="级别"></el-table-column>
                                            <el-table-column width="150" property="clientMobile" label="手机号码"></el-table-column>
                                            <el-table-column property="clientSite" label="地址"></el-table-column>
                                        </el-table>
                                        <el-pagination
                                                style="margin-left: 350px;"
                                                layout="prev, pager, next"
                                                :page-size="pageSize"
                                                @current-change="handleCurrentChange"
                                                :current-page.sync="currPage"
                                                :total="totalSize">
                                        </el-pagination>
                                        <div style="text-align: center;width: 100%" class="dialog-footer">
                                            <el-button type="primary" @click="xzkehu">保 存</el-button>
                                            <el-button type="primary" @click="kehu = false">取 消</el-button>
                                        </div>
                                        <el-input v-if="this.xg==0" v-model="jhlist.clientId.clientName" placeholder="+添加" slot="reference" :readonly='true' style="width: 250px;"></el-input>
                                    </el-popover>
                                    <el-input v-if="this.xg==1" :disabled="true" v-model="jhlist.clientId.clientName" placeholder="+添加" :readonly='true' style="width: 250px;"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="8">
                                <el-form-item style="width:250px" prop="deliveryId">
                                    <label slot="label" style="font-size: 13px">交付计划：</label><br/>
                                    <el-popover
                                            style="width: 700px; height: 450px;"
                                            placement="right"
                                            trigger="click"
                                            v-model="dindan">
                                        <span style="font-size: 16px;">关联计划模块</span>
                                        <br/>
                                        <el-input @keyup.enter.native="cx2" v-model="dindaninput" style="width: 210px; margin-left: 230px; margin-bottom: 10px;">
                                            <i slot="suffix" class="el-input__icon el-icon-search" @click="cx2"></i>
                                        </el-input>
                                        <el-button type="primary" style="margin-left: 100px;">新 建</el-button>
                                        <el-table
                                                highlight-current-row
                                                border
                                                ref="dindanlist"
                                                :data="dindanlist">
                                            <el-table-column type="selection" width="50" align="center" property="deliveryId"></el-table-column>
                                            <el-table-column width="150" property="deliveryTheme" label="主题"></el-table-column>
                                            <el-table-column width="200" property="orderId.orderTheme" label="订单"></el-table-column>
                                            <el-table-column width="100" property="deliveryMoney" label="总金额"></el-table-column>
                                            <el-table-column width="150" property="deliveryIssue" label="期次"></el-table-column>
                                        </el-table>
                                        <el-pagination
                                                style="margin-left: 350px;"
                                                layout="prev, pager, next"
                                                :page-size="pageSize2"
                                                :current-page.sync="currPage2"
                                                @current-change="handleCurrentChange2"
                                                :total="totalSize2">
                                        </el-pagination>
                                        <div style="text-align: center;width: 100%" class="dialog-footer">
                                            <el-button type="primary" @click="xzdd">保 存</el-button>
                                            <el-button type="primary" @click="dindan = false">取 消</el-button>
                                        </div>
                                        <el-input  v-if="this.xg==0" v-model="jhlist.deliveryId.deliveryTheme" @focus="dakai" placeholder="+添加" slot="reference" :readonly='true' style="width: 250px;"></el-input>
                                    </el-popover>
                                    <el-input v-if="this.xg==1" :disabled="true" v-model="jhlist.deliveryId.deliveryTheme" placeholder="+添加" :readonly='true' style="width: 250px;"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="10">
                                <el-form-item style="width:250px" prop="chukuCanku">
                                    <label slot="label" style="font-size: 13px">仓库：</label><br/>
                                    <el-select v-model="jhlist.chukuCanku" placeholder="请选择" style="width: 250px">
                                    <el-option
                                            v-for="item in options"
                                            :key="item.value"
                                            :label="item.label"
                                            :value="item.value">
                                    </el-option>
                                </el-select>
                                </el-form-item>
                            </el-col>
                            <el-col :span="8">
                                <el-form-item style="width:250px">
                                    <label slot="label" style="font-size: 13px">负责人：</label><br/>
                                    <el-input v-model="jhlist.chukuFuzeren" :disabled="true"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <br/>
                    <span style="margin-left:30px;font-size: 13px">产品</span>
                    <br/>
                    <br/>
                    <el-table :data="jhlist.xq" style="width: 80%;margin-left: 10px;font-size:13px">
                        <el-table-column
                                align="center"
                                prop="prodetailId.productName"
                                label="产品名称">
                        </el-table-column>
                        <el-table-column
                                align="center"
                                label="产品规格">
                            <template slot-scope="s">
                                <span v-for="pro in s.row.prodetailId.qmodeldetails" :key="pro.prodetailId">
                                    {{pro.modetContent}}
                                </span>
                            </template>
                        </el-table-column>
                        <el-table-column
                                align="center"
                                prop="chukuNumber"
                                label="数量">
                        </el-table-column>

                    </el-table>
                    <br/>
                    <div style="margin-left:540px;font-size: 12px;margin-top: 10px;">
                        <span>总件数：{{this.shuliang}}</span>
                    </div>
                    <br/>
                </el-scrollbar>
                <!--确定按钮 设置了固定定位-->
                <div style="text-align: center;width: 80%;position: absolute;top:635px;left:230px;" class="dialog-footer">
                    <el-button  @click="tzdz">取 消</el-button>
                    &nbsp;&nbsp;
                    <el-button type="primary" @click="addbusiness('jhlist')">确 定</el-button>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
    import {formatTimeToStr} from '../router//format.js'
    export default {
        name: "Zxinzenchuku",
        data() {
            var hueikuan = (rule, value, callback) => {
            if (this.jhlist.clientId.clientName === null || this.jhlist.clientId.clientName === '') {
                callback(new Error('请先选择客户'));
            } else {
                if (this.jhlist.deliveryId.deliveryTheme === '' || this.jhlist.deliveryId.deliveryTheme === null) {
                    callback(new Error('请选择计划'));
                }else {
                    callback()
                }
            }
        };
            var kehua = (rule, value, callback) => {
                if (value.clientName === null || value.clientName === '') {
                    callback(new Error('请选择客户'));
                }else {
                    callback()
                }
            };
            return {
                rules: {
                    clientId: [
                        {validator: kehua, required: true, trigger: ['blur','change']},
                    ],
                    deliveryId: [
                        {validator: hueikuan, required: true, trigger: ['blur','change']},
                    ],
                    chukuTime: [
                        {required: true, message: '请选择签约时间', trigger: 'change' },
                    ],
                    chukuCanku: [
                        {required: true, message: '请选择出库仓库', trigger: 'change' },
                    ]
                },
                jhlist:{
                        chukuId:0,
                        deliveryId:{
                            deliveryId:0,
                            deliveryTheme:null,
                        },
                        clientId:{
                            clientId:0,
                            clientName:null
                        },
                        chukuNo:null,
                        chukuCanku:null,
                        chukuTime:null,
                        chukuShifouth:'否',
                        chukuFuzeren:null,
                        chukuFahuo:'未发货',
                        xq:[]
                    },
                kehu:false,//客户弹出框
                kehuinput:'',//客户弹框搜索框
                kehulist:[],//客户表格信息
                pageSize:5,//客户分页页大小
                currPage:1,//客户当前页码，默认为1
                totalSize:0,//客户总记录数
                dindan:false,//订单弹出框
                dindaninput:'',//订单弹框搜索框
                dindanlist:[],//订单表格信息
                pageSize2:5,//订单分页页大小
                currPage2:1,//订单当前页码，默认为1
                totalSize2:0,//订单总记录数
                pickerOptions: {//开票日期快捷选择方式
                    shortcuts: [{
                        text: '今天',
                        onClick(picker) {
                            picker.$emit('pick', new Date());
                        }
                    }, {
                        text: '昨天',
                        onClick(picker) {
                            const date = new Date();
                            date.setTime(date.getTime() - 3600 * 1000 * 24);
                            picker.$emit('pick', date);
                        }
                    }, {
                        text: '一周前',
                        onClick(picker) {
                            const date = new Date();
                            date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
                            picker.$emit('pick', date);
                        }
                    }]
                },
                xg:0,
                options: [{
                    value: '全国仓库',
                    label: '全国仓库'}],
                shuliang:0,
                ys:1,
                ydx:10,
                dz:''
            };
        },
        methods: {
            tzdz(){
                if(this.dz==='htzy'){
                    this.$router.push({
                        path:'/heton',
                        query: {ys:this.ys,ydx:this.ydx}
                    });
                }else {
                    this.$router.push({
                        path:'/chuku',
                        query: {ys:this.ys,ydx:this.ydx}
                    });
                }
            },
            /*当前页方法*/
            handleCurrentChange(val) {
                this.initData(val);
            },
            /*当前页方法*/
            handleCurrentChange2(val) {
                this.jihuainitData(val);
            },
            cx(){
                this.initData(this.currPage);
            },
            cx2(){
                this.jihuainitData(this.currPage2)
            },
            //确定按钮
            addbusiness(formName) {
                if(this.xg!=0){
                    this.xzfapiao();
                }else{
                    this.$refs[formName].validate(valid => {
                        this.print(valid);
                        window.console.log(valid)
                        if (valid) {
                            this.xzfapiao();
                        }else{
                            this.$message({
                                message: '验证不通过！',
                                type: 'warning'
                            });
                            return false;
                        }

                    });
                }
            },
            /*初始化商机编号*/
            initbusNo() {
                var date = new Date();
                var year = date.getFullYear();
                var month = date.getMonth() + 1;
                var day = date.getDate();
                if (month < 10) {
                    month = "0" + month;
                    if (day < 10) {
                        day = "0" + day;
                    }
                    //使用年月日作为中间数
                    var busnosj = year + month + day;

                    //使用时间戳作为最后五位随机数
                    var a = date.getTime() + "";
                    var b = a.substring(8, 13);

                    //最后编号
                    var busno = "CK_" + busnosj + "_" + b;
                    this.jhlist.chukuNo = busno;
                }
            },
            //根据交付计划ID查询交付计划详情所有产品信息
            jhxqinitData(){
                this.$axios.post("http://localhost:8088/jf/list_jhxq",this.qs.stringify(
                    {search:this.jhlist.deliveryId.deliveryId}))
                    .then(v=>{
                        v.data.forEach((a,i)=>{
                            const htxq = {};
                            htxq.chukuxqId=0;
                            htxq.prodetailId=null;
                            htxq.chukuNumber=null;
                            htxq.chukuKetueinumber=null;
                            htxq.chukuJg=null;
                            this.$set(htxq,'chukuxqId',0);
                            this.$set(htxq,'prodetailId',a.prodetailId);
                            this.$set(htxq,'chukuNumber',a.deliverydeliveryNumber);
                            this.$set(htxq,'chukuKetueinumber',a.deliverydeliveryNumber);
                            this.$set(htxq,'chukuJg',a.deliverydeliveryJg);
                            this.shuliang+=a.deliverydeliveryNumber;
                            this.jhlist.xq.splice(i,htxq,0);
                        })
                    })
                    .catch()
            },
            ckxq(){
                this.$axios.post("http://localhost:8088/chuku/list_chukuxq",this.qs.stringify(
                    {search:this.jhlist.chukuId}))
                    .then(v=>{
                        this.jhlist.xq = v.data;
                        v.data.forEach((a)=>{
                            this.shuliang+=a.chukuNumber;
                        })
                    })
                    .catch()
            },
            //点击合同输入框就查询合同信息
            dakai() {
                this.jihuainitData(this.currPage2);
            },
            //选择订单确定按钮
            xzdd() {
                this.$refs.dindanlist.selection.forEach((pro)=>{
                    this.jhlist.deliveryId=pro;
                });
                this.jhlist.xq=[];
                this.shuliang=0;
                this.jhxqinitData();
                this.dindan=false;
                this.dindaninput='';
            },
            //订单查询方法
            jihuainitData(dd) {
                this.$axios.post("http://localhost:8088/jf/list_jh",this.qs.stringify(
                    {search3:'出库',search:this.dindaninput,search2:this.jhlist.clientId.clientId, page:dd, size:this.pageSize2,}))
                    .then(v=>{
                        this.dindanlist = v.data.rows;
                        this.totalSize2 = v.data.total;
                    })
                    .catch()
            },
            //选择客户确定按钮
            xzkehu() {
                this.$refs.kehulist.selection.forEach((pro) => {
                    this.jhlist.clientId = pro;
                });
                this.kehu = false;
                this.kehuinput = '';
            },
            //客户查询方法
            initData(dd) {
                this.$axios.post("http://localhost:8088/dindan/kehu_list", this.qs.stringify(
                    {search: this.kehuinput, page: dd, size: this.pageSize,}))
                    .then(v => {
                        this.kehulist = v.data.rows;
                        this.totalSize = v.data.total;
                    })
                    .catch()
            },
            xzfapiao(){
                this.$axios.post("http://localhost:8088/chuku/add",this.jhlist)
                    .then(()=>{
                        if(this.xg==1){
                            this.$message({
                                showClose: true,
                                type: 'success',
                                message: '修改成功!',
                            });
                        }else {
                            this.$message({
                                showClose: true,
                                type: 'success',
                                message: '新增成功!',
                            });
                        }
                        this.$router.push({
                            path:'/zhuye/jiaofu'
                        });
                    }).catch(()=>{
                        this.$message({
                            showClose: true,
                            message: '修改失败',
                            type: 'error'
                        });
                    }
                )
            }
        },
        created: function () {
            this.initData(this.currPage);
            this.initbusNo();
            this.jhlist.chukuFuzeren = JSON.parse(localStorage.getItem("user")).empName;
            if(this.$route.query.key!=null){
                this.jhlist=this.$route.query.key;
                this.xg = 1;
                this.ckxq()
            }
            if(this.$route.query.ys!=null){
                this.ys=this.$route.query.ys;
            }
            if(this.$route.query.ydx!=null){
                this.ydx=this.$route.query.ydx;
            }
            if(this.$route.query.dz!=null){
                this.dz=this.$route.query.dz;
            }
        },
        filters: {
            formatDate: function (time) {
                if (time != null && time != "") {
                    var date = new Date(time);
                    return formatTimeToStr(date, "yyyy-MM-dd");
                } else {
                    return "";
                }
            }
        }
    }
</script>

<style scoped>
    .shugan{
        border-left-color: rgb(70, 205, 207);
        border-left-width: 1.8px;
        height: 10px;
        border-left-style: solid;
        display: inline;
        font-size: 13px;
        margin-left: 25px;
    }

    /deep/ .el-scrollbar__wrap {
        overflow: scroll;
        width: 120%;
        height: 100%;
    }

</style>